﻿using Microsoft.Extensions.DependencyInjection;
using Model.Core.Entity.Dbcontexts;
using Model.Core.Services;
using RobotPratolSolution.Models;
using System.Collections.ObjectModel;

using CommunityToolkit.Mvvm.ComponentModel;

namespace RobotPratolSolution.ViewModels
{
    public class TaskStationListViewModel:ObservableObject
    {
        LocalRobotInfo LocalRobotInfo { get; set; }
        DbcontextBase _DbcontextBase { get; set; }
           private string _RobotType;
        public string RobotType
        {
            get => _RobotType;
            set{  _RobotType = value; OnPropertyChanged(); }
}

        public TaskStationListViewModel()
        {
            _DbcontextBase = App.ServiceProvider.GetService<DbcontextBase>();
            LocalRobotInfo = App.ServiceProvider.GetService<LocalRobotInfo>();
            //RobotType = 
        }

        public ObservableCollection<RobotCtrlModel> _RobotTaskCtrls { get; set; } = new ObservableCollection<RobotCtrlModel>();
        public ObservableCollection<JobInfoModel> JobInfoModels { get; set; }
        public ObservableCollection<StationModelView> StationModels { get; set; }

        public string _SelectedJobId { get; set; } = string.Empty;

        /// <summary>
        /// 任务列表刷新
        /// </summary>
        public void JobListInfoRefresh()
        {

            var datalist = _DbcontextBase.JobInfo.Where(a => a.RobotId == LocalRobotInfo.RobotId).ToList();
            if (JobInfoModels == null) JobInfoModels = new ObservableCollection<JobInfoModel>();
            else
                JobInfoModels.Clear();
            foreach (var item in datalist)
            {
                JobInfoModels.Add(new JobInfoModel()
                {
                    Name = item.Name,
                    Id = item.Id.ToString(),
                    JobType = item.JobType,
                });
            }
        }
        public void LoadRobotInfo()
        {
            RobotType= LocalRobotInfo.RobotType;
        }

        public void TaskListDetailListRefresh()
        {
            if (string.IsNullOrEmpty(_SelectedJobId)) return;
            Guid guid = Guid.Parse(_SelectedJobId);
            var datalist = _DbcontextBase.JobNodeInfo.Where(a => a.jobid == guid).OrderBy(a => a.ordernum).ToList();
            if (_RobotTaskCtrls == null) _RobotTaskCtrls = new ObservableCollection<RobotCtrlModel>();
            else
                _RobotTaskCtrls.Clear();

            //datalist = datalist.OrderBy(a => a.ordernum);
            for (int i = 0; i < datalist.Count(); i++)
            {
                var item = datalist[i];
                _RobotTaskCtrls.Add(new RobotCtrlModel()
                {
                    Name = item.Name,
                    Id = item.Id.ToString(),
                    CtrlType = item.ActionType,
                    Order = item.ordernum,
                    data = item.ActionData
                });

            }

        }
        public void StationInfoRefresh()
        {
            var datalist = _DbcontextBase.PointInfo.Where(a => a.MapId.Id.ToString() == LocalRobotInfo.MapId).ToList();
            if (StationModels == null) StationModels = new ObservableCollection<StationModelView>();
            else
                StationModels.Clear();
            foreach (var item in datalist)
            {
                StationModels.Add(new StationModelView()
                {
                    Name = item.Name,
                    Id = item.Id.ToString(),
                    X = item.X,
                    Y = item.Y,
                    Z = item.Angle
                });
            }


        }
        public void addMoveCtrl(StationModelView station, int PointType)
        {
            int order = 0;
            if (_RobotTaskCtrls.Count > 0)
                order = _RobotTaskCtrls.ToList().Max(a => a.Order) + 1;
            Guid guid = Guid.NewGuid();
            string CtrlType = "move";
            string Namestr = "移动";
            switch (PointType)
            {
                case 0:
                    Namestr = "移动->点=>";
                    CtrlType = "move";
                    break;
                case 1:
                    Namestr = "移动->点=>";
                    CtrlType = "movexy";
                    break;
                case 2:
                    Namestr = "移动->角度=>";
                    CtrlType = "moveangle";
                    break;
                case 4:
                    Namestr = "移动->点(沿直线)=>";
                    CtrlType = "movexyinline";
                    break;
                case 5:
                    Namestr = "前进一米(沿直线)=>";
                    CtrlType = "moveadvance";
                    break;
                case 6:
                    Namestr = "去往上货箱点=>";
                    CtrlType = "align_with_rack";
                    break;
                case 7:
                    Namestr = "去往卸载货箱点=>";
                    CtrlType = "to_unload_point";
                    break;


            }
            _RobotTaskCtrls.Add(new RobotCtrlModel()
            {
                Name = Namestr + station?.Name,
                CtrlType = CtrlType,
                Id = guid.ToString(),
                Order = order,
                data = "" + station?.Id,
            });
            _DbcontextBase.JobNodeInfo.Add(new Model.Core.Entity.JobNodeInfo()
            {
                Id = guid,
                Name = Namestr + station?.Name,
                jobid = Guid.Parse(_SelectedJobId),
                ActionType = CtrlType,
                IsDelete = false,
                ordernum = order,
                ActionData = "" + station?.Id,

            });
            _DbcontextBase.SaveChanges();

        }
        public void AddRobotDoCharge()
        {
            int order = 0;
            if (_RobotTaskCtrls.Count > 0)
                order = _RobotTaskCtrls.ToList().Max(a => a.Order) + 1;
            Guid guid = Guid.NewGuid();
            _RobotTaskCtrls.Add(new RobotCtrlModel()
            {
                Name = "充电动作",
                CtrlType = "docharge",
                Id = guid.ToString(),
                Order = order,
                data = "docharge",
            });
            _DbcontextBase.JobNodeInfo.Add(new Model.Core.Entity.JobNodeInfo()
            {
                Id = guid,
                Name = "充电动作",
                jobid = Guid.Parse(_SelectedJobId),
                ActionType = "docharge",
                IsDelete = false,
                ordernum = order,
                ActionData = "docharge",

            });
            _DbcontextBase.SaveChanges();

        }


        public void AddJackCtrl(bool isup)
        {
            int order = 0;
            if (_RobotTaskCtrls.Count > 0)
                order = _RobotTaskCtrls.ToList().Max(a => a.Order) + 1;
            string CtrlType = isup ? "jack_up" : "jack_down";
            Guid guid = Guid.NewGuid();
            _RobotTaskCtrls.Add(new RobotCtrlModel()
            {
                Name = "顶升装置=>" + (isup ? "上升" : "下降"),
                CtrlType = CtrlType,
                Id = guid.ToString(),
                Order = order,
                data = isup.ToString(),
            });

            _DbcontextBase.JobNodeInfo.Add(new Model.Core.Entity.JobNodeInfo()
            {
                Id = guid,
                Name = "顶升装置=>" + (isup ? "上升" : "下降"),
                jobid = Guid.Parse(_SelectedJobId),
                ActionType = CtrlType,
                IsDelete = false,
                ordernum = order,
                ActionData = isup.ToString(),

            });
            _DbcontextBase.SaveChanges();
        }



        public void AddSpinMotror(string isOk)
        {
            int order = 0;
            if (_RobotTaskCtrls.Count > 0)
                order = _RobotTaskCtrls.ToList().Max(a => a.Order) + 1;
            Guid guid = Guid.NewGuid();
            _RobotTaskCtrls.Add(new RobotCtrlModel()
            {
                Name = "旋转电机=>" + isOk,
                CtrlType = "SpinMotor",
                Id = guid.ToString(),
                Order = order,
                data = isOk.ToString(),
            });

            _DbcontextBase.JobNodeInfo.Add(new Model.Core.Entity.JobNodeInfo()
            {
                Id = guid,
                Name = "旋转电机=>" + isOk,
                jobid = Guid.Parse(_SelectedJobId),
                ActionType = "SpinMotor",
                IsDelete = false,
                ordernum = order,
                ActionData = isOk.ToString(),

            });
            _DbcontextBase.SaveChanges();
        }


        public void DeleteItem(int Index)
        {
            var data = _RobotTaskCtrls[Index];
            var nodeinfo = _DbcontextBase.JobNodeInfo.FirstOrDefault(a => a.Id.ToString() == data.Id);
            _DbcontextBase.Remove(nodeinfo);
            _DbcontextBase.SaveChanges(true);
            _RobotTaskCtrls.RemoveAt(Index);
        }
        public void AddMotror(string data)
        {
            int order = 0;
            if (_RobotTaskCtrls.Count > 0)
                order = _RobotTaskCtrls.ToList().Max(a => a.Order) + 1;
            Guid guid = Guid.NewGuid();
            _RobotTaskCtrls.Add(new RobotCtrlModel()
            {
                Name = "电机=>" + data,
                CtrlType = "Motor",
                Id = guid.ToString(),
                Order = order,
                data = data.ToString(),
            });

            _DbcontextBase.JobNodeInfo.Add(new Model.Core.Entity.JobNodeInfo()
            {
                Id = guid,
                Name = "电机=>" + data,
                jobid = Guid.Parse(_SelectedJobId),
                ActionType = "Motor",
                IsDelete = false,
                ordernum = order,
                ActionData = data.ToString(),

            });
            _DbcontextBase.SaveChanges();
        }

        public void AddRedLigtOpen(string data)
        {
            int order = 0;
            if (_RobotTaskCtrls.Count > 0)
                order = _RobotTaskCtrls.ToList().Max(a => a.Order) + 1;
            Guid guid = Guid.NewGuid();
            _RobotTaskCtrls.Add(new RobotCtrlModel()
            {
                Name = "红灯提示3秒" + data,
                CtrlType = "redlight",
                Id = guid.ToString(),
                Order = order,
                data = data.ToString(),
            });

            _DbcontextBase.JobNodeInfo.Add(new Model.Core.Entity.JobNodeInfo()
            {
                Id = guid,
                Name = "红灯提示3秒" + data,
                jobid = Guid.Parse(_SelectedJobId),
                ActionType = "redlight",
                IsDelete = false,
                ordernum = order,
                ActionData = data.ToString(),

            });
            _DbcontextBase.SaveChanges();
        }


        public void UpJobNodeFunc(int index)
        {

            var data1 = _RobotTaskCtrls[index];
            var data2 = _RobotTaskCtrls[index - 1];

            var jobnode1 = _DbcontextBase.JobNodeInfo.FirstOrDefault(a => a.Id.ToString() == data1.Id);
            var jobnode2 = _DbcontextBase.JobNodeInfo.FirstOrDefault(a => a.Id.ToString() == data2.Id);
            int ordernum = jobnode2.ordernum;
            jobnode2.ordernum = jobnode1.ordernum;
            jobnode1.ordernum = ordernum;



            data2.Order = jobnode2.ordernum;
            data1.Order = jobnode1.ordernum;

            _DbcontextBase.Update(jobnode1);
            _DbcontextBase.Update(jobnode2);
            _DbcontextBase.SaveChanges(true);
            _RobotTaskCtrls.RemoveAt(index);
            _RobotTaskCtrls.Insert(index - 1, data1);

        }
        public void DownJobNodeFunc(int index)
        {
            var data1 = _RobotTaskCtrls[index];
            var data2 = _RobotTaskCtrls[index + 1];

            var jobnode1 = _DbcontextBase.JobNodeInfo.FirstOrDefault(a => a.Id.ToString() == data1.Id);
            var jobnode2 = _DbcontextBase.JobNodeInfo.FirstOrDefault(a => a.Id.ToString() == data2.Id);
            int ordernum = jobnode2.ordernum;
            jobnode2.ordernum = jobnode1.ordernum;
            jobnode1.ordernum = ordernum;

            data2.Order = jobnode2.ordernum;
            data1.Order = jobnode1.ordernum;

            _DbcontextBase.Update(jobnode1);
            _DbcontextBase.Update(jobnode2);
            _DbcontextBase.SaveChanges(true);
            _RobotTaskCtrls.RemoveAt(index + 1);
            _RobotTaskCtrls.Insert(index, data2);
        }
    }
}
